import React, { useState, useEffect } from 'react';
import { getLocalUserInfo } from 'utils/util';
import service from 'service';

export const HomeContext = React.createContext({});

export default (props) => {
    const [user, setUser] = useState(getLocalUserInfo()); // 用户信息
    const [type, setType] = useState('add'); // 编辑类型 [add,edit,delete]
    const [visible, setVisible] = useState(false); // 新增|编辑弹窗
    const [viewPassVisible, setViewPassVisible] = useState(false); // 查看密码
    const [viewMoreVisible, setViewMoreVisible] = useState(false); // 查看更多
    const [record, setRecord] = useState(null);
    const [labels, setLabels] = useState([]);
    const [list, setList] = useState([]);
    const [searchText, setSearchText] = useState('');

    // 新增或者编辑时[显示弹窗,设置操作类型,设置表单数据]
    const editHandle = (visible, type, data) => {
        setVisible(visible)
        setType(type)
        setRecord(data)
    }
    // 查看密码弹窗[显示弹窗,设置数据]
    const viewPassHandle = (visible, data) => {
        setViewPassVisible(visible)
        setRecord(data)
    }
    // 查看详情弹窗[显示弹窗设置数据]
    const viewMoreHandle = (visible, data) => {
        setViewMoreVisible(visible)
        setRecord(data)
    }

    async function getLabels() {
        service.label.findAll().then(data => {
            setLabels(data)
        })
    }
    async function getList() {
        service.account.findAll()
            .then(data => {
                let temp = data.map(item => {
                    item.tags = JSON.parse(item.tags || [])
                    return item;
                })
                setList(temp);
            })
            .finally(() => {
                console.log('account-请求账户列表-请求完毕')
            })
    }

    const value = {
        user, setUser,
        visible, setVisible,
        viewPassVisible, setViewPassVisible,
        viewMoreVisible, setViewMoreVisible,
        type, setType,
        record, setRecord,
        labels, setLabels,
        list, setList,
        searchText, setSearchText,

        getLabels,
        getList,

        editHandle,
        viewPassHandle,
        viewMoreHandle,
    }

    return value;
    // return (
    //     <HomeContext.Provider value={value}>
    //         {props.children}
    //     </HomeContext.Provider>
    // )
}